package week_03.coef_additive;

import org.junit.Test;

import java.util.Scanner;

@SuppressWarnings("all")
public class function {
    /**
     * 将需要放入数据的数组传进来，因为有4个数组需要数据，创建此方法以方便代码的复用
     *
     * @param n
     * @return
     */
    public int[] arr(int[] n) {
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        while (i < n.length) {

            int num = scanner.nextInt();
            n[i] = num;
            i++;
        }
        return n;
    }


    /**
     * 根据输入的长度生成相应长度的多项式的系数
     *
     * @param n 返回coeff数组
     */
    public int[] coeff_length(int n) {
        int i = 0;
        int[] coeff = new int[n];
        while (i < n) {
            coeff[i] = i;
            i++;
        }
        return coeff;
    }

    /**
     * 根据长度生成响应长度的指数
     *
     * @param n 返回pow数组
     * @return
     */
    public int[] pow_length(int n) {
        int i = 0;
        int[] pow = new int[n];
        while (i < n) {
            pow[i] = i;
            i++;
        }
        return pow;
    }

    /**
     * @param a 传进来的系数数组
     * @param b 传进来的指数数组
     */
    public String function(int[] a, int[] b) {
        int i = 0;
        String s = a[i] + "* Math.pow(x," + b[i] + ") + ";
        if (a.length > b.length) {
            while (i < a.length) {
                String t = a[i] + "* Math.pow(x," + b[i] + ") + ";
                s = s + t;
                i++;
            }
        } else {
            while (i < b.length) {
                String t = a[i] + "* Math.pow(x," + b[i] + ") + ";
                s = s + t;
                i++;
            }
        }

        return s;
    }

    /**
     * @param s1_coeff 传入的第一个多项式的系数
     * @param s1_pow   传入的第一个多项式的指数
     * @param s2_coeff 传入的第二个多项式的系数
     * @param s2_pow   传入第二个多项式的指数
     * @return
     */
    public String f_add(int[] s1_coeff, int[] s1_pow, int[] s2_coeff, int[] s2_pow) {
        int i = 0;
        String s = ":";
        String t;
        while (i < s1_coeff.length || i < s2_coeff.length) {
            if (s1_coeff.length == s2_coeff.length) {
                if (s1_pow[i] == s2_pow[i]) {
                    s1_coeff[i] = s1_coeff[i] + s2_coeff[i];
                    t = s1_coeff[i] + "* Math.pow(x," + s1_pow[i] + ") + ";
                    s = s + t;
                    i++;
                } else {
                    if (s1_pow[i] > s2_pow[i]) {
                        t = s1_coeff[i] + "* Math.pow(x," + s1_pow[i] + ") + " + s2_coeff[i] + "* Math.pow(x," + s2_pow[i] + ") + ";
                        s = s + t;
                    } else {
                        t = s2_coeff[i] + "* Math.pow(x," + s2_pow[i] + ") + " + s1_coeff[i] + "* Math.pow(x," + s1_pow[i] + ") +";
                        s = s + t;
                    }
                    i++;
                }
            } else if (s1_coeff.length > s2_coeff.length) {
                while (i < s1_coeff.length) {
                    t = s1_coeff[i] + "* Math.pow(x," + s1_pow[i] + ") +";
                    s = s + t;
                    i++;
                }
            } else if (s1_coeff.length < s2_coeff.length) {
                while (i < s2_coeff.length) {
                    t = s2_coeff[i] + "* Math.pow(x," + s2_pow[i] + ") +";
                    s = s + t;
                    i++;
                }
            }
        }

        return s;
    }
}
